package cn.itsource.service.impl;

import cn.itsource.config.anno.MyLog;
import cn.itsource.domain.BlogArticleType;
import cn.itsource.domain.html.NumberOfArticlesByCategory;
import cn.itsource.mapper.ArticleTypeMapper;
import cn.itsource.query.BlogArticleTypeQuery;
import cn.itsource.service.BlogArticleTypeService;
import cn.itsource.util.UserTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service //交给spring容器管理
public class BlogArticleTypeServiceImpl implements BlogArticleTypeService {

    /**
     * 注入mapper层
     */
    @Autowired
    private ArticleTypeMapper rticleTypeMapper;

   @Override
    public PageInfo<BlogArticleType> pageList(BlogArticleTypeQuery blogArticleTypeQuery) {
        //分页核心方法
        PageHelper.startPage(blogArticleTypeQuery.getPage(),blogArticleTypeQuery.getRows());


        //紧接着查询所有，中间不能写别的代码
       List<BlogArticleType> blogArticleTypes = rticleTypeMapper.pageList(blogArticleTypeQuery);

       //封装成pageInfo
       PageInfo<BlogArticleType> blogArticlePageInfo = new PageInfo(blogArticleTypes);

       //返回数据
        return blogArticlePageInfo;
    }

    @Override
    @MyLog(value = "文章类型")
    public BlogArticleType findOne(Long id) {

        //使用tk的方法查询一个
        return rticleTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogArticleType> findAll() {

        Example example = new Example(BlogArticleType.class);
        example.and().andEqualTo("status","1");

        return rticleTypeMapper.selectByExample(example);
    }

    @Override
    @MyLog(value = "文章类型")

    public void save(BlogArticleType blogArticleType) {
        blogArticleType.setUpdateUserName(UserTools.getCurrentUserRealName());
        blogArticleType.setUpdateTime(new Date());
        //只会新增有值的字段
        rticleTypeMapper.insertSelective(blogArticleType);
    }

    @Override
    @MyLog(value = "文章类型")

    public void update(BlogArticleType blogArticleType) {
        //        修改时间
        blogArticleType.setUpdateTime(new Date());
        rticleTypeMapper.updateByPrimaryKeySelective(blogArticleType);
    }

    @Override
    @MyLog(value = "文章类型",isImportant = true)

    public void delete(Long id) {
        //根据主键id删除一个对象
        rticleTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<NumberOfArticlesByCategory> selectArticleTypeStatistics() {
       return rticleTypeMapper.selectArticleTypeStatistics();
    }

}

